<template>
  <div class="login">
    登录页
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ required: true, message: '请填写密码' }]"
      />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit"
          >提交</van-button
        >
      </div>
    </van-form>
  </div>
</template>


<script>
import instance from "@/api";

import debounce from "@/utils/debounce.js";

import {Toast} from "vant";

export default {
  name: "login",
  data() {
    return {
      username: "",
      password: "",
      newdebouncefn: null,
      origin: null,
    };
  },
  created() {
    this.newdebouncefn = debounce(this.loginfun);
  },
  methods: {
    async loginfun() {
      let { data } = await instance.post("/login");
      console.log(data);
      this.$store.commit("settoken", data.token);


      let parames = this.$route.params.name
      Toast({
        type: "success",
        message: "登录成功",
        onClose: () => {
          this.$router.push({ name: parames});
        },
      });
    },
    onSubmit(values) {
      this.newdebouncefn();

      
    },
  },
};
</script>